<?php

class Application_Model_DbTable_Product extends Zend_Db_Table_Abstract
{

    protected $_name = 'products';
    
     protected $_dependentTables = array(
        "Application_Model_DbTable_Comment"
    );
    
    protected $_referenceMap = array(
        "Category" => array(
            'columns' => "category",
            'refTableClass' => "Application_Model_DbTable_ProductCategory",
            'refColumns'=>"id",
            'onUpdate' => self::RESTRICT,
            'onDelete' => self::RESTRICT
        )
    );
    
    public function fetchPaginatorAdapter($filters=array(), $sortField = null, $sortType="asc")
    {
        $select = $this->select();
        //filter
        if(count($filters)>0)
        {
            foreach($filters as $field=>$filter)
            {
                if(!empty($filter) && !empty($field))
                {
                    if(is_string($filter))
                    {                      
                        $select->where("$field LIKE '%$filter%'");
                        if($field=="name")
                        {
                             $select->orWhere("code LIKE '%$filter%'");
                        }
                    }
                    else
                    {  
                        $select->where("$field=?", $filter);
                    }
                }               
            }
        }
        
        //sort
        if(null != $sortField)
        {
            $select->order($sortField." ".$sortType);
        }
        
        $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
        return $adapter;
    }
    
    public function changeType($choice, $id)
    {
        $row = $this->find($id)->current();
        $nstatus = ($row->{$choice})?0:1;
        $row->{$choice} = $nstatus;
        return $row->save();
    }

}

